<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.real_name"
        placeholder="姓名"
        style="width: 130px;"
        class="filter-item"
      />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter" >
        查询
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="unionMerchant()" >
        新增
      </el-button>
      <excelout style="margin-top:-1px"></excelout>
    </div>

    <el-table :key="tableKey" v-loading="listLoading" :data="list"  border fit highlight-current-row style="width: 100%;">
      <el-table-column label="客户姓名" align="center" width="200">
        <template slot-scope="{row}">
          <span>{{ row.real_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商户名称" align="center" width="400">
        <template slot-scope="{row}">
          <span>{{ row.merchant_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center">
        <template slot-scope="{row}">
          <span>{{ row.desc }}</span>
        </template>
      </el-table-column>
     
      <el-table-column label="操作" align="center" width="120" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
        
          <el-button type="primary"  @click="descc(row)">
            备注
          </el-button>
          <!-- <el-button  @click="unionMerchant(row.channel_id)">
            关联医院
          </el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />


    <el-dialog title="备注" :visible.sync="dialogFormVisible" width="960px">
      <el-form ref="dataForm"   label-position="left" label-width="120px" style="width: 70%; margin-left:70px;">
        <el-form-item label="备注" prop="name">
          <el-input v-model="temp.desc" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="addDescHold()">
          确认
        </el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="unionMerchantable" title="订单选择" >
      <template>
        <el-transfer
          filterable
          filter-placeholder="请输入客户名称"
          v-model="choose"
          :titles="title"
          target-order="unshift"
          :data="order_list">
        </el-transfer>
      </template> 
      <span slot="footer" class="dialog-footer">
        <el-button  @click="unionMerchantable = false">
          取消
        </el-button>
        <el-button type="primary"   @click="transferToHold()">
          确定
        </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

import {
  holdOrderList,transferToHold,addDescHold,holdorderExcelout
} from '@/api/channel'
import {
  getBackendSignOrderAll
} from '@/api/order'
import {
  formatDate
} from '@/utils/formatDate.js'
import waves from '@/directive/waves' // waves directive
import { merchantListOnlyMerchant } from '@/api/merchant'
import {
  parseTime,
  getNodes
} from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import excelout from '@/components/excelout/index.vue'
export default {
  name: 'ComplexTable',
  components: {
    Pagination,excelout
  },
  directives: {
    waves
  },
  filters: {
    formatDate(time) {
      time = time * 1000
      const date = new Date(time)

      return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
    }
  },
  data() {
    return {
      nodes: [],
      title:['所有订单', '已选择'],
      tableKey: 0,
      tableRecord: 1,
      list: null,
      record_list: null,
      listRecordLoading: true,
      recordTotal: 0,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        real_name: '',
        mobile: '',
        idcard: '',
        order_status: [1,3],
        city: '',
      },
      choose:[],
      order_list:[],
      temp: {
        desc: '',
      },
      listRecordQuery: {
        page: 1,
        limit: 20,
        order_id: ''
      },
      city: [],
      textMap: {
        record: '订单记录',
        create: 'Create'
      },
      dialogFormVisible: false,
      dialogRecordVisible: false,
      dialogStatus: '',
      unionMerchantable:false,
      downloadLoading: false,
      listMerchantQuery: {
          page: 1,
          limit: 500
        },
    }
  },
  created() {
    this.getList();
  },
 
  methods: {
    excelout() {
      this.listLoading = true
      this.listQuery.limit = this.total;
      holdorderExcelout(this.listQuery).then(response => {
        this.listLoading = false
        const filepath = response.data.path
        window.location.href = filepath
      })
    },
    closeDialog(){
      this.dialogFormVisible = false;
      this.order_data = [];
      this.choose = [];
    },
    unionMerchant(id){
        this.unionMerchantable = true;
        this.getBackendSignOrderAll();
        //this.getOrderPaymentOverList(row);
    },
    getList() {
      this.listLoading = true
      holdOrderList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        setTimeout(() => {
          this.listLoading = false
        }, 1.0 * 1000)
      })
    },
    descc(row) {
      this.dialogFormVisible = true; 
      this.temp.desc = row.desc;
      this.temp.id = row.id;
    },
    handleCreate() {
      this.temp.name = '';
      this.temp.bank_number = '';
      this.temp.channel_id = '';
      this.temp.bank_address = '';
      this.temp.bank_person = '';
      this.dialogFormVisible = true; 
    },
    handleFilter() {
      this.listLoading = true;
      this.listQuery.page = 1;
      this.getList();
    },
    addDescHold(){
      addDescHold(this.temp).then(response => {
        this.listLoading = true
        if(response.code==200){
          this.listLoading = false
          this.dialogFormVisible = false
          this.$notify({
              title: 'Success',
              message: '完成！',
              type: 'success',
              duration: 2000,
              onClose: this.getList()
            })
        }
      })
    },
    getBackendSignOrderAll() {
      this.order_list = [];
      getBackendSignOrderAll(this.listMerchantQuery).then(response => {
        let d = response.data;
        
        for(let i=0;i<d.length;i++){
          //let a = {key:d[i].merchant_id,label:d[i].sort_name,search: d[i].real_name}
          this.order_list.push ({key:d[i].order_id,label:d[i].name+'-'+d[i].logo_name+'-'+d[i].real_name,search: d[i].real_name});
        }
        //console.log(this.merchant_list) 
        
      })
    },
    transferToHold(){
      
      if(this.choose.length==0){
        this.$notify({
          title: '提示',
          message: '请选择订单',
          type: 'warning',
          duration:2000
        });
        return;
      }
      let param = {choose:this.choose};
      transferToHold(param).then(res => {
        this.listLoading = false
        if(res.code==200){
          this.$notify({
            title: '提示',
            message: '完成',
            type: 'success',
            duration:2000
          });
          this.closeDialog();
          this.getList()
        }
        
      })
    },
  }
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    white-space: nowrap;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
    float: left;
  }
  .el-transfer-panel{
        width: 240px;
        height: 400px;
    }
    .el-transfer-panel__list.is-filterable{
        height: 400px;
    }

</style>
